/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI.bll;

import BLL.Student;
import BLL.homepage;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 *
 * @author YELIUHUISHI
 */
public class Add extends javax.swing.JFrame {

    /**
     * Creates new form Ad
     */
    public Add() {
        initComponents();
        init();
    }

    private void init() {
        this.setTitle("添加界面");
        this.setResizable(false);
        uiutlLogin uiutlLogin = new uiutlLogin();
        uiutlLogin.setFrameConter(this);
        uiutlLogin.setFrameTmage(this,"src"+ File.separator+ "UI" +File.separator+"icon"+File.separator+"add.jpg");
        new Thread(new runnableTime(this.Time)).start();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        addID = new javax.swing.JLabel();
        addName = new javax.swing.JTextField();
        addSex = new javax.swing.JComboBox();
        addAge = new javax.swing.JTextField();
        addAdd = new javax.swing.JToggleButton();
        addCancel = new javax.swing.JButton();
        Time = new javax.swing.JLabel();

        Time.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        Time.setForeground(new java.awt.Color(255, 20, 18));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("id");

        jLabel2.setText("name");

        jLabel3.setText("sex");

        jLabel4.setText("age");

        addName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNameActionPerformed(evt);
            }
        });

        addSex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "男", "女" }));

        addSex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSexActionPerformed(evt);
            }
        });

        addAge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddActionPerformed(evt);
            }
        });

        addAdd.setText("添加");
        addAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAddActionPerformed(evt);
            }
        });

        addCancel.setText("取消");
        addCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addAdd)
                        .addGap(18, 18, 18)
                        .addComponent(addCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addAge)
                            .addComponent(addSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addName)
                            .addComponent(addID)))
                    .addComponent(Time, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(addID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(addName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(addSex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(addAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addAdd)
                    .addComponent(addCancel))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNameActionPerformed
        addSex.requestFocus();
    }//GEN-LAST:event_addNameActionPerformed

    private void addSexActionPerformed(java.awt.event.ActionEvent evt){
        addAge.requestFocus();
    }

    //添加键的动作事件
    private void addAddActionPerformed(java.awt.event.ActionEvent evt) {
        //获取添加的内容
        String userAddName=addName.getText().trim();
        String userAddSex= (String) addSex.getSelectedItem();
        String userAddAge=addAge.getText().trim();

        //规则
        String userNameRegex="^([\\u4e00-\\u9fa5]{1,20}|[a-zA-Z\\.\\s]{1,20})$";//验证姓名正则
        String userAgeRegex="^[0-9]+([0-9]+){0,1}$";

        if(!userAddName.matches(userNameRegex)){
            JOptionPane.showMessageDialog(this,"姓名不符合规范");
            addName.setBackground(new Color(255, 22, 32));
            this.addName.requestFocus();
            return;
        }
        if(!userAddAge.matches(userAgeRegex)){
            JOptionPane.showMessageDialog(this,"年龄不符");
            addName.setBackground(new Color(209, 231, 255));
            addAge.setBackground(new Color(255, 22, 32));
            this.addAge.requestFocus();
            return;
        }

        homepage homepage = new homepage();
        Student student = new Student(userAddName,userAddSex,Integer.valueOf(userAddAge));
        boolean add = homepage.add(student);


        new Homepage().setVisible(true);
        this.dispose();
    }

    //取消键的动作事件
    private void addCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCancelActionPerformed
        new Homepage().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_addCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JToggleButton addAdd;
    public javax.swing.JTextField addAge;
    public javax.swing.JButton addCancel;
    public javax.swing.JLabel addID;
    public javax.swing.JTextField addName;
    public javax.swing.JComboBox addSex;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel Time;
    // End of variables declaration//GEN-END:variables
}
